package datastructure.array;

/**
 * Rotate a one-dimensional array of n elements to the right by k steps.
 * For instance, with n=7 and k=3, the array {a, b, c, d, e, f, g} is
 * rotated to {e, f, g, a, b, c, d}
 *
 * @author Lei Zhang
 */
public class RotateArrayInPlace {

    public static void rotateArrayInPlace(int[] array, int k) {

        int n = array.length;
        GeneralFunctions.reverse(array, 0, n - 1);
        GeneralFunctions.reverse(array, k, n - 1);
        GeneralFunctions.reverse(array, 0, k - 1);
    }

    public static void main(String args[]) {

        int[] anArray1 = {
            1,
            2,
            3,
            4,
            5,
            6
        };

        RotateArrayInPlace.rotateArrayInPlace(anArray1, 3);

        for (int i = 0; i < anArray1.length; i++) {
            System.out.println(anArray1[i]);
        }
    }
}
